import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-11
 * Time: 8:38
 */
class MyThreadPool{
    //线程队列
    public BlockingQueue<Runnable>  queue = new ArrayBlockingQueue<>(1000);
    //线程池中线程数量,线程执行Runnable中描述 的任务
    public MyThreadPool(int capacity){
        for (int i = 0; i <capacity ; i++) {
            Thread t=new Thread(()->{
                try {
                    while(true){
                        Runnable runnable =queue.take();
                        runnable.run();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }

    }
    //向线程池中提交任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);

    }
}
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool=new MyThreadPool(100);
        for (int i = 0; i < 10000; i++) {
            int id=i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello  "+id+", "+Thread.currentThread().getName());
                }
            });
        }
    }
}
